<?PHP
include("settings.php");
include("connect.php");
include("functions.php");
include("header.php");

echo '<div id="header">
<ul id="tabnav">
	<li><a href="index.php">Home</a></li>
	'.($settings['personaljournal'] == true ? "<li><a href=\"journal.php\">Personal Journal</a></li>" : "").'
	'.($settings['personaltransactions'] == true ? "<li><a href=\"transactions.php\">Personal Transactions</a></li>" : "").'
	'.($settings['corpjournal'] == true ? "<li><a href=\"corpj.php\">Corporate Journal</a></li>" : "").'
	'.($settings['corptransactions'] == true ? "<li><a href=\"corpt.php\">Corporate Transactions</a></li>" : "").'
	<li id="time">'.get_current_time().'</li>
</ul>
</div>';

echo '
<table id="wrapper" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td colspan="7">';

if ($_GET['wallet'] == 'personaltransactions' && $settings['personaltransactions'] == true) {
	$url = "http://api.eve-online.com/char/WalletTransactions.xml.aspx";
	get_api($url);
	$xml = simplexml_load_file($xmlfile);
	unlink($xmlfile);
	if ($xml->error) {
		echo $xml->error[code].'<br />';
		echo $xml->error.'<br />';
		log_error(array($info,$xml->error));
	}
	else {
		$n = 0;
		foreach ($xml->result->rowset->row as $row) {
		$query21 = "
INSERT IGNORE INTO `".$settings[mysql_real_escape_string($_GET['wallet']).table]."` (`transactionDateTime`,`transactionID`,`quantity`,`typeName`,`typeID`,`price`,`clientID`,`clientName`,`stationID`,`stationName`,`transactionType`) VALUES ('".mysql_real_escape_string($row[transactionDateTime])."','".mysql_real_escape_string($row[transactionID])."','".mysql_real_escape_string($row[quantity])."','".mysql_real_escape_string($row[typeName])."','".mysql_real_escape_string($row[typeID])."','".mysql_real_escape_string($row[price])."','".mysql_real_escape_string($row[clientID])."','".mysql_real_escape_string($row[clientName])."','".mysql_real_escape_string($row[stationID])."','".mysql_real_escape_string($row[stationName])."','".mysql_real_escape_string($row[transactionType])."')";
		if ($result = $db->query($query21)) {
		$n++;
		}
		}
		echo '<tr><td id="row" colspan="7" style="text-align:center;height:75px;">
			'.$n.' entries added.
		  </td></tr>';
	}
} # End Personal Transactions XML

if ($_GET['wallet'] == 'personaljournal' && $settings['personaljournal'] == true) {
	$url = "http://api.eve-online.com/char/WalletJournal.xml.aspx";
	get_api($url);
	$xml = simplexml_load_file($xmlfile);
	unlink($xmlfile);
	if ($xml->error) {
		echo $xml->error[code].'<br />';
		echo $xml->error.'<br />';
		log_error(array($info,$xml->error));
	}
	else {
		$n = 0;
		foreach ($xml->result->rowset->row as $row) {
		$query22 = "
INSERT IGNORE INTO `".$settings[mysql_real_escape_string($_GET['wallet']).table]."` (`date`,`refID`,`refTypeID`,`ownerName1`,`ownerID1`,`ownerName2`,`ownerID2`,`argName1`,`argID1`,`amount`,`balance`,`reason`) VALUES ('".mysql_real_escape_string($row[date])."','".mysql_real_escape_string($row[refID])."','".mysql_real_escape_string($row[refTypeID])."','".mysql_real_escape_string($row[ownerName1])."','".mysql_real_escape_string($row[ownerID1])."','".mysql_real_escape_string($row[ownerName2])."','".mysql_real_escape_string($row[ownerID2])."','".mysql_real_escape_string($row[argName1])."','".mysql_real_escape_string($row[argID1])."','".mysql_real_escape_string($row[amount])."','".mysql_real_escape_string($row[balance])."','".mysql_real_escape_string($row[reason])."')";
		if ($result = $db->query($query22)) {
		$n++;
		}
		}
		echo '<tr><td id="row" colspan="7" style="text-align:center;height:75px;">
			'.$n.' entries added.
		  </td></tr>';
	}
} # End Personal Journal XML

# Run Corp Transactions XML and insert into database
if ($_GET['wallet'] == 'corptransactions' && $settings['corptransactions'] == true) {
	if (isset($_GET['division']) && !empty($_GET['division']))
		{
		$settings['divisions'] = $_GET['division'];
		}
		foreach ($settings['divisions'] as $division) {
		$data['accountKey'] = $division;
	$url = "http://api.eve-online.com/corp/WalletTransactions.xml.aspx";
	get_api($url);
	$xml = simplexml_load_file($xmlfile);
	unlink($xmlfile);
	if ($xml->error) {
		echo $xml->error[code].'<br />';
		echo $xml->error.'<br />';
		log_error(array($info,$xml->error));
	}
	else {
		$n = 0;
		foreach ($xml->result->rowset->row as $row) {
		$query23 = "
INSERT IGNORE INTO `".$settings[mysql_real_escape_string($_GET['wallet']).table]."` (`transactionDateTime`,`transactionID`,`quantity`,`typeName`,`typeID`,`price`,`clientID`,`clientName`,`characterID`,`characterName`,`stationID`,`stationName`,`transactionType`,`division`) VALUES ('".mysql_real_escape_string($row['transactionDateTime'])."','".mysql_real_escape_string($row['transactionID'])."','".mysql_real_escape_string($row['quantity'])."','".mysql_real_escape_string($row['typeName'])."','".mysql_real_escape_string($row['typeID'])."','".mysql_real_escape_string($row['price'])."','".mysql_real_escape_string($row['clientID'])."','".mysql_real_escape_string($row['clientName'])."','".mysql_real_escape_string($row['characterID'])."','".mysql_real_escape_string($row['characterName'])."','".mysql_real_escape_string($row['stationID'])."','".mysql_real_escape_string($row['stationName'])."','".mysql_real_escape_string($row['transactionType'])."','".mysql_real_escape_string($division)."')";
		if ($result = $db->query($query23)) {
		$n++;
		}
		}
		echo '<tr><td id="row" colspan="7" style="text-align:center;height:75px;">
			'.$n.' entries added.
		  </td></tr>';
	}
	}
} # End Corp Transactions XML

# Run Corp Journal XML and insert into database
if ($_GET['wallet'] == 'corpjournal' && $settings['corpjournal'] == true) {
	if (isset($_GET['division']) && !empty($_GET['division']))
		{
		$settings['divisions'] = $_GET['division'];
		}
		foreach ($settings['divisions'] as $division) {
		$data['accountKey'] = $division;
	$url = "http://api.eve-online.com/corp/WalletJournal.xml.aspx";
	get_api($url);
	$xml = simplexml_load_file($xmlfile);
	unlink($xmlfile);
	if ($xml->error) {
		echo $xml->error[code].'<br />';
		echo $xml->error.'<br />';
		log_error(array($info,$xml->error));
	}
	else {
		$n = 0;
		foreach ($xml->result->rowset->row as $row) {
		$query24 = "
INSERT IGNORE INTO `".$settings[mysql_real_escape_string($_GET['wallet']).table]."` (`date`,`refID`,`refTypeID`,`ownerName1`,`ownerID1`,`ownerName2`,`ownerID2`,`argName1`,`argID1`,`amount`,`balance`,`reason`,`division`) VALUES ('".mysql_real_escape_string($row[date])."','".mysql_real_escape_string($row[refID])."','".mysql_real_escape_string($row[refTypeID])."','".mysql_real_escape_string($row[ownerName1])."','".mysql_real_escape_string($row[ownerID1])."','".mysql_real_escape_string($row[ownerName2])."','".mysql_real_escape_string($row[ownerID2])."','".mysql_real_escape_string($row[argName1])."','".mysql_real_escape_string($row[argID1])."','".mysql_real_escape_string($row[amount])."','".mysql_real_escape_string($row[balance])."','".mysql_real_escape_string($row[reason])."','".mysql_real_escape_string($division)."')";
		if ($result = $db->query($query24)) {
		$n++;
		}
		}
		echo '<tr><td id="row" colspan="7" style="text-align:center;height:75px;">
			'.$n.' entries added.
		  </td></tr>';
	}
	}
} # End Corp Journal XML

echo '
		</td>
	</tr>
</table>';
?>